﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace Documentanalysis
{
    public partial class Select : Form
    {
        public List<string> folderArray;
        public string searchCondition = "";
        public SendMessage sendMessageToWaitingForm;
        public Waiting waiting;
        public bool needOpen = true;

        private List<string> dirList = new List<string>();
        private List<string> fileList = new List<string>();
        public Select()
        {
            InitializeComponent();
        }

        private void Select_Shown(object sender, EventArgs e)
        {
            Thread t2 = new Thread(new ParameterizedThreadStart(doTask));
            t2.IsBackground = true;
            t2.Start("");

            waiting = new Waiting();
            sendMessageToWaitingForm += waiting.getMessageFromTask;
            waiting.t2 = t2;
            //waiting.path = folderName;
            if (needOpen)
            {
                waiting.ShowDialog();
            }
        }

        private void getDir(DirectoryInfo dir)
        {
            if (dir.Name.IndexOf(searchCondition) >= 0) {
                dirList.Add(dir.FullName);
            }
           
            FileInfo[] files = dir.GetFiles();
            for (var i = 0; i < files.Count(); i++)
            {
                try
                {
                    if (files[i].Name.IndexOf(searchCondition) >= 0)
                    {
                        fileList.Add(files[i].FullName);
                    }
                }
                catch (Exception) { }
            }

            DirectoryInfo[] dirs = dir.GetDirectories();
            for (var i = 0; i < dirs.Count(); i++)
            {
                if (IsSystemHidden(dirs[i]) == false)
                {
                    try
                    {
                        getDir(dirs[i]);
                    }
                    catch (Exception) { }
                }
            }
            
        }

        private void doTask(object data)
        {
            getDir(new DirectoryInfo(folderArray[0]));

            try
            {
                listView1.BeginInvoke(new MethodInvoker(() =>
                {
                    for (var i = 0; i < dirList.Count(); i++)
                    {
                        ListViewItem li = new ListViewItem();
                        li.ImageIndex = 0;
                        li.SubItems[0].Text = dirList[i];
                        li.SubItems.Add("");
                        listView1.Items.Add(li);
                    }

                    for (var i = 0; i < fileList.Count(); i ++)
                    {
                        ListViewItem li = new ListViewItem();
                        li.SubItems[0].Text = fileList[i];
                        li.SubItems.Add("");
                        listView1.Items.Add(li);
                    }
                }));
            }
            catch (Exception) { MessageBox.Show("更新界面错误!"); }

            //for (int i = 0; i < folderArray.Count; i++)
            //{
            //    string[] paths = new string[0];
            //    string[] files = new string[0];
            //    DirectoryInfo dir = new DirectoryInfo(folderArray[i]);
            //    try
            //    {
                    
            //        DirectoryInfo[] xx = dir.GetDirectories(searchCondition, SearchOption.AllDirectories);
            //    }
            //    catch (Exception) { MessageBox.Show("无法检索目录"); }
            //    try
            //    {
            //        files = Directory.GetFiles(folderArray[i], searchCondition, SearchOption.AllDirectories);
            //    }
            //    catch (Exception) { MessageBox.Show("无法检索文件"); }

            //    try
            //    {
            //        listView1.BeginInvoke(new MethodInvoker(() =>
            //        {
            //            foreach (string s in paths)
            //            {
            //                ListViewItem li = new ListViewItem();
            //                li.ImageIndex = 0;
            //                li.SubItems[0].Text = s;
            //                li.SubItems.Add("");
            //                listView1.Items.Add(li);
            //            }

            //            foreach (string s in files)
            //            {
            //                ListViewItem li = new ListViewItem();
            //                li.SubItems[0].Text = s;
            //                li.SubItems.Add("");
            //                listView1.Items.Add(li);
            //            }
            //        }));
            //    }
            //    catch (Exception) { MessageBox.Show("更新界面错误!"); }
            //}

            needOpen = false;
            if (sendMessageToWaitingForm != null)
            {
                sendMessageToWaitingForm();
            }

        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(listView1.SelectedItems[0].Text);
        }

        private static bool IsSystemHidden(DirectoryInfo dirInfo)
        {
            if (dirInfo.Parent == null)
            {
                return false;
            }
            string attributes = dirInfo.Attributes.ToString();
            if (attributes.IndexOf("Hidden") > -1 && attributes.IndexOf("System") > -1)
            {
                return true;
            }
            return false;
        }
    }
}
